import {defineConfig, loadEnv} from 'vite'
import vue from '@vitejs/plugin-vue'
import {fileURLToPath, URL} from 'url'
import unocss from 'unocss/vite'
import {presetIcons, presetUno} from 'unocss'
// 按需引入
import {VantResolver} from 'unplugin-vue-components/resolvers'
import Components from 'unplugin-vue-components/vite';

// Vite 默认是不加载 .env 文件的，所以如果想要配置不同换将请求不同接口所以需要配置.env文件执行
export default defineConfig(({command, mode, ssrBuild}) => {
  const env = loadEnv(mode, process.cwd(), '')

  return {
    plugins: [
      vue(),
      unocss({
        presets: [presetIcons(), presetUno()]
      }),
      Components({
        resolvers: [VantResolver()],
      }),
    ],
    resolve: {
      extensions: ['.mjs', '.js', '.ts', '.jsx', '.tsx', '.json', '.less', '.css'],
      alias: {
        '@': fileURLToPath(new URL('./src', import.meta.url))
      }
    },
    // 配置ip访问
    server: {
      host: '0.0.0.0',
      port: 4000,
      // 是否开启 https
      https: false,
      // 配置跨域
      proxy: {
        '/wyy': {
          target: 'https://yucloud.website',
          changeOrigin: true,
          rewrite: (path) => path.replace(/^\/wyy/, '/wyy')
        },
      }
    },
    define: {
      'process.env': env
    }
  }
})
